package com.anmai.crawler1.service;

import com.anmai.crawler1.dao.ImgDao;
import com.anmai.crawler1.entity.ImgBean;
import com.anmai.crawler1.utils.JsoupUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
public class PhotoService {


    @Autowired
    private ImgDao imgDao;

    public void getLocation() throws IOException, InterruptedException {
        for(int i=0;i<29;i++){
            BufferedInputStream accept = JsoupUtils.getConnection("http://photo2018.usts.edu.cn/?P="+i)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cache-Control", "max-age=0")
                    .execute().bodyStream();
//            Thread.sleep(100);
            Document doc = Jsoup.parse(accept, "gb2312", "http://photo2018.usts.edu.cn/?P=0");
            Elements elements = doc.select("body table tbody tr td table tbody tr td");
            //遍历tr
            for(Element e : elements){
                if(e.selectFirst("a") != null) {
                    String url =  e.selectFirst("a").attr("href").replaceFirst("\\\\", "");
                    ImgBean imgBean = new ImgBean();
                    imgBean.setImg(url);
                    imgBean.setStatus(0);
                    //判断是否存在
                    if(imgDao.isExist(url) == 0){
                        imgDao.add(imgBean);
                    }
                }
            }

        }
    }

    /**
     * 获取本地下载链接
     * @throws IOException
     * @throws InterruptedException
     */
    public static void getLocalList() throws IOException {

        //判断下载文件链接在不在，不在先获取下载列表
        String absolutePath = new File("").getAbsolutePath();
        File fileList = new File(absolutePath + "/list.txt");

        if (!fileList.exists()) {
            fileList.createNewFile();
            System.out.println("获取下载列表...");
            BufferedWriter writer  = new BufferedWriter(new FileWriter(fileList,true));
            List<String> list = new ArrayList<>();
            for(int i=0;i<29;i++){
                BufferedInputStream accept = JsoupUtils.getConnection("http://photo2018.usts.edu.cn/?P="+i)
                        .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                        .header("Accept-Encoding", "gzip, deflate")
                        .header("Accept-Language", "zh-CN,zh;q=0.9")
                        .header("Cache-Control", "max-age=0")
                        .execute().bodyStream();
                Document doc = Jsoup.parse(accept, "gb2312", "http://photo2018.usts.edu.cn/?P=0");
                Elements elements = doc.select("body table tbody tr td table tbody tr td");
                //遍历tr

                for(Element e : elements){
                    if(e.selectFirst("a") != null) {
                        String url =  e.selectFirst("a").attr("href").replaceFirst("\\\\", "");
                        list.add(url);
                    }
                }

            }
           list.stream().forEach(bean -> {
               try {
                   writer.write(new String(bean.getBytes(),"utf-8"));
                   writer.newLine();
               } catch (IOException e1) {
                   e1.printStackTrace();
               }
           });
            writer.flush();
            writer.close();
        }else {
            System.out.println("继续下载...");
        }

    }

    //本地爬取图片
    public void localDownloadImg() throws Exception {
        String absolutePath = new File("").getAbsolutePath();
        File fileList = new File(absolutePath + "/list.txt");
        /*
        File file = new File("E:imgs");
        if (!file.exists()) {
            file.mkdir();
        }
        List<ImgBean> imgBeans = imgDao.queryByStatus(0);
        imgBeans.stream().forEach(imgBean -> {
            //下载图片
            //图片的保存路径
            try {
                String[] split = imgBean.getImg().split("\\\\");
                String filePath="E://imgs/"+ split[0]+"/"+imgBean.getId() +"_"+split[1]+".jpg";
                String url = "http://photo2018.usts.edu.cn/"+ URLEncoder.encode(split[0],"utf-8")+"/"
                        +split[1];
                getImage(url,filePath);
                //更新状态
                imgBean.setStatus(1);
                imgDao.update(imgBean);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });*/
    }

    public static void test(String[] args) throws IOException {
        getLocalList();
    }

    public static void main(String[] args) throws Exception {
//            getLocalList();
        String absolutePath = new File("").getAbsolutePath();
        File fileList = new File(absolutePath + "/list.txt");

        while (true) {
            //获取当前可下载
            List<String> list = new ArrayList<>();
            BufferedReader reader = new BufferedReader(new FileReader(fileList));
            String line = reader.readLine();
            while(line!=null){
                line = reader.readLine();
                list.add(line);
            }
            reader.close();
            //开启下载
            if (list != null && list.size() > 0) {
                String name = list.get(0);
                String[] split = name.split("\\\\");
                String filePath="E://imgs/"+ split[0]+"/"+split[1]+".jpg";
                String url = "http://photo2018.usts.edu.cn/"+ URLEncoder.encode(split[0],"utf-8")+"/"
                        +split[1];
                getImage(url,filePath);
                //重新写入数据
                BufferedWriter writer  = new BufferedWriter(new FileWriter(fileList,false));
                list.stream().forEach(bean -> {
                    try {
                        if(!StringUtils.isEmpty(bean)){
                            writer.write(new String(bean.getBytes(),"utf-8"));
                            writer.newLine();
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                });
                writer.flush();
                writer.close();
            }else {
                break;
            }
        }
    }

    //爬取图片
    public void downloadImg() throws Exception {
        File file = new File("E:imgs");
        if (!file.exists()) {
            file.mkdir();
        }
        List<ImgBean> imgBeans = imgDao.queryByStatus(0);
        imgBeans.stream().forEach(imgBean -> {
            //下载图片
            //图片的保存路径
            try {
                String[] split = imgBean.getImg().split("\\\\");
                String filePath="E://imgs/"+ split[0]+"/"+imgBean.getId() +"_"+split[1]+".jpg";
                String url = "http://photo2018.usts.edu.cn/"+ URLEncoder.encode(split[0],"utf-8")+"/"
                        +split[1];
                getImage(url,filePath);
                //更新状态
                imgBean.setStatus(1);
                imgDao.update(imgBean);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
    }


    public static void getImage(String urlpath,String filePath) throws Exception{  //首先得到请求路径,并抛出异常


        try {
            URL url = new URL(urlpath);  //建立url 类对象，抛异常
            HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //得到urlConnection对象
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36");

            conn.setRequestMethod("GET");     //声明请求方式
            conn.setConnectTimeout(60000 * 1000);    //设置连接超时
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();   //得到一个输入流

                //从服务端返回数据，相对客户端为流入
                byte[] data = readInstream(inputStream);  //得到数据
                File file = new File(filePath);   //创建保存文件
                File fileParent = file.getParentFile();
                if (!fileParent.exists()) {
                    fileParent.mkdirs();
                }
                FileOutputStream outputStream = new FileOutputStream(file);//创建一个文件的输出流
                outputStream.write(data);  //把所有的二进值数据全部写入到我们建好的文件中
                outputStream.flush();
                outputStream.close();   //关闭输出流
//            System.out.println("下载完成！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //读取流文件的内容
    private static byte[] readInstream(InputStream inputStream) throws Exception {

        ByteArrayOutputStream byteArrayOutputStream =
                new ByteArrayOutputStream(); //创建ByteArrayOutputStream 对象
        byte[] buffer = new byte[1024];   //声明缓冲区
        int length = -1;         //定义读取默认长度

        while((length=inputStream.read(buffer))!=-1){
            byteArrayOutputStream.write(buffer,0,length);
            //把缓存区中输出到内存中
        };
        byteArrayOutputStream.close();  //关闭输出流
        inputStream.close();          //关闭输入流

        return byteArrayOutputStream.toByteArray();
        //返回这个输出流的字节数组
    }

}
